<template>
	<div class="mt-4" v-loading="data.mtloading">
		<el-form ref="formRef" label-width="100px">

			<el-form-item v-for="(item, index) in data.list" :key="index" :label="item.title">
				<el-input v-model="item.value" class="input_width" :placeholder="$t('message.pleaseEnter')" />
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="onSubmit" v-if="dtasObj.config_id" v-prmis="'configs_update'">{{
					$t('public.ok') }}</el-button>
				<el-button type="primary" @click="onSubmit" v-prmis="'configs_add'" v-else>{{
					$t('public.ok') }}</el-button>
				<el-button @click="onCancel">{{ $t('public.cancel') }}</el-button>
			</el-form-item>

		</el-form>
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import { getconfig } from '@/api/system';
let dtasObj = ref({})
let emit = defineEmits(['infoData'])
let data = reactive({
	mtloading: true,
	config: {
		config_name: 'tempKey',
		config_content: '',
		config_switch: 1,
		csr_id: 0,
		config_id: 0,
	},
	list: {
		stockTmpId: { title: $t('mock.templateList'), value: '' },
		faultId: { title: $t('mock.fault'), value: '' },
		onLineId: { title: $t('mock.online'), value: '' },
		offLineId: { title: $t('mock.Offline'), value: '' },
		purchaseId: { title: $t('mock.successPurch'), value: '' },
		balance: { title: $t('mock.notice'), value: '' },
		recharge: { title: $t('mock.recharge'), value: '' },
		e_id: { title: $t('mock.card'), value: '' },
		commodityExpiration: { title: $t('mock.expiration'), value: '' }
	}
})

// 获取信息
let getmessage = () => {
	data.mtloading = true
	getconfig({ config_name: 'tempKey' }).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			let content = JSON.parse(res.data.config_content)
			for (let key in data.config) {
				data.config[key] = res.data[key]
			}
			for (let key in data.list) {
				data.list[key].value = content[key]
			}
			data.config.csr_id = res.data.creator
		}
	})
}

// 保存
let onSubmit = () => {
	let notice = {}
	for (let key in data.list) {
		notice[key] = data.list[key].value
	}
	let datas = {
		config_name: 'tempKey',
		config_content: JSON.stringify(notice),
		config_switch: data.config.config_switch,
		csr_id: data.config.csr_id
	}
	if (data.config.config_id != 0) {
		datas.config_id = data.config.config_id
	}
	dtasObj.value = datas
	emit('infoData', datas)
}

// 取消
let onCancel = () => {
	getmessage()
}

getmessage()

// 暴露方法 方便父组件调用
defineExpose({
	getmessage
})

</script>

<style lang="scss" scoped>
.input_width {
	width: 600px;
}
</style>
